package com.gary.springcloud.controller;

import ch.qos.logback.core.util.TimeUtil;
import com.gary.springcloud.entity.CommonResult;
import com.gary.springcloud.entity.Payment;
import com.gary.springcloud.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author pengrui
 * @date 2020/12/30 - 16:00
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private IPaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String port;

    @PostMapping("/payment/save")
    public CommonResult<Integer> save(@RequestBody Payment payment) {
        Integer row = paymentService.save(payment);
        log.info("****插入结果"+payment);
        CommonResult<Integer> result;
        if (row > 0) {
            result = new CommonResult<>(200, "添加成功, port = "+port, row);
        } else  {
            result = new CommonResult<>(400, "添加失败", row);
        }
        return result;
    }

    @GetMapping("/payment/query/{id}")
    public CommonResult<Payment> queryById(@PathVariable("id") Long id) {
        Payment payment = paymentService.queryById(id);
        log.info("****查询结果"+payment+"\t"+"O(∩_∩)O哈哈~");
        CommonResult<Payment> result;
        if (payment != null) {
            result = new CommonResult<>(200, "查询成功 port = "+port, payment);
        } else {
            result = new CommonResult<>(400, "查询失败，无记录", null);
        }
        return result;
    }

    @GetMapping("/payment/feign/timeout")
    public CommonResult<String> timeout() {
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        log.info("timeout调用成功。。。");
        return new CommonResult<>(200,"hystrix timeout success");
    }

    @GetMapping("/payment/hystrix/ok/{id}")
    public CommonResult<String> hystrixOk(@PathVariable("id") Integer id) {
        log.info("ok调用成功。。。" + id);
        return new CommonResult<>(200,id + "hystrix ok success");
    }

    @GetMapping("/payment/hystrix/timeout")
    public CommonResult<String> hystrixTimeout() {
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        return new CommonResult<>(200,"success");
    }

    @GetMapping("/payment/dis")
    public Object discovery() {

        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("*** element : " + element);
            List<ServiceInstance> instances = discoveryClient.getInstances(element);
            for (ServiceInstance instance : instances) {
                log.info("*** instance : " + instance);
                log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
            }
        }
        return discoveryClient;
    }


    /**
     * 测试自定义负载均衡算法
     * @return port
     */
    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        return port;
    }
}
